package com.joey.cloud.flowable.controller;

import cn.hutool.core.util.StrUtil;
import com.joey.cloud.communal.utils.ResponseUtil;
import com.joey.cloud.communal.vo.PageLongVo;
import com.joey.cloud.communal.vo.PageIntVo;
import com.joey.cloud.flowable.service.FlowUserService;
import com.joey.cloud.flowable.vo.FlowDefinitionVo;
import com.joey.cloud.flowable.vo.FlowProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.bpmn.model.UserTask;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Api(tags = "flow历史管理")
@Controller
@RequestMapping(value = "flowable/his")
@Slf4j
public class FlowableHisController {
    //操作流程定义相关  删除 部署流程定义等
    @Autowired
    RepositoryService repositoryService;
    //操作流程实例的
    @Autowired
    RuntimeService runtimeService;
    //操作任务相关的
    @Autowired
    TaskService taskService;
    //查询历史表数据的
    @Resource
    HistoryService historyService;
    @Resource
    FlowUserService flowUserService;


    @ApiOperation(value = "获取流程实例列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value="用户id",paramType="query"),
            @ApiImplicitParam(name="type",value="1=发起的2=参与的",required=true,dataType = "integer",defaultValue = "1",paramType="query"),
            @ApiImplicitParam(name="pageNo",value="第几页，从1开始",required=true,paramType="query",dataType = "integer",defaultValue = "1"),
            @ApiImplicitParam(name="pageSize",value="每页显示数",required=true,paramType="query",dataType = "integer",defaultValue = "20")
    })
    @GetMapping(value = "queryProcessList")
    @ResponseBody
    public ResponseUtil<PageLongVo<FlowProcessVo>> queryProcessList(String userId,int type,int pageNo,int pageSize){
        try {
            List<FlowProcessVo> processVoList = new ArrayList<>();
            HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();
            if(StrUtil.isNotEmpty(userId)&&type==1){
                query.startedBy(userId);
            }
            if(StrUtil.isNotEmpty(userId)&&type==2){
                query.involvedUser(userId);
            }
            query.orderByProcessInstanceStartTime().desc();
            List<HistoricProcessInstance> list = query.listPage((pageNo - 1) * pageSize, pageSize);
            for(HistoricProcessInstance item : list){
                FlowProcessVo vo = new FlowProcessVo(item);
                vo.setStartUserName(flowUserService.queryNameByUserIds(vo.getStartUserId()));
                processVoList.add(vo);
            }
            return ResponseUtil.success(PageLongVo.create(query.count(),processVoList));
        }catch (Exception e){
            log.error("获取流程实例列表出错",e);
            return ResponseUtil.error();
        }
    }

    @ApiOperation(value = "获取流程定义列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNo",value="第几页，从1开始",required=true,paramType="query",dataType = "integer",defaultValue = "1"),
            @ApiImplicitParam(name="pageSize",value="每页显示数",required=true,paramType="query",dataType = "integer",defaultValue = "20")
    })
    @GetMapping(value = "queryDefinitionList")
    @ResponseBody
    public ResponseUtil<PageLongVo<FlowDefinitionVo>> queryDefinitionList(int pageNo,int pageSize){
        try {
            List<FlowDefinitionVo> processVoList = new ArrayList<>();
            ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery().latestVersion();
            List<ProcessDefinition> list  = query.listPage((pageNo - 1) * pageSize, pageSize);
            for(ProcessDefinition item : list){
                FlowDefinitionVo vo = new FlowDefinitionVo(item);
                processVoList.add(vo);
            }
            return ResponseUtil.success(PageLongVo.create(query.count(),processVoList));
        }catch (Exception e){
            log.error("获取流程定义列表出错",e);
            return ResponseUtil.error();
        }
    }


}
